package cn.czxy.service.zy.user;

import cn.czxy.dao.zy.user.UserDao;
import cn.czxy.domain.User;

import java.io.File;
import java.util.List;

/**
 * @author zhangyuan@163.com
 * @date 2019/4/12
 * @infos
 */
public class UserService {

    private UserDao userDao = new UserDao();

    private static String rootPath = null;

    static {
        String t = UserService.class.getClassLoader().getResource("/").getPath();
        File file = new File( t);
        rootPath = file.getParentFile().getParentFile().getAbsolutePath();
    }

    /**
     * 添加用户的方法
     * @param user
     * @return
     */
    public boolean addUser(User user) {
        List<User> list = userDao.readAll();
        boolean falg = false;
        if (list!=null)
        {
            for (User user1 : list) {
                if (user1.getUserName().equals(user.getUserName()))
                {
                    falg = true;
                    break;
                }
            }
        }
        if (!falg) {
            initUser(user);
            userDao.addUser(user);
            return true;
        }else
        {
            return false;
        }
    }

    /**
     * 初始化user类
     * @param user
        注意用户名不能为空
     */
    public void initUser(User user)
    {
        user.setMaxScope(300);
        user.setCurrentScope(100);
        user.setType("普通用户");
        user.setPath(rootPath+"\\"+user.getUserName());

        //创建文件夹
        set1(user.getUserName());

    }

    public void set1(String userName)
    {
        String path =rootPath+"\\"+"files";
        File file = new File(path + "\\"+userName);
        file.mkdir();

        File file1 = new File(path + "\\"+userName+"\\"+"imges");
        File file2 = new File(path + "\\"+userName+"\\"+"music");
        File file3 = new File(path + "\\"+userName+"\\"+"other");
        File file4 = new File(path + "\\"+userName+"\\"+"texts");
        File file5 = new File(path + "\\"+userName+"\\"+"videos");

        file1.mkdir();
        file2.mkdir();
        file3.mkdir();
        file4.mkdir();
        file5.mkdir();
    }


    /**
     * 用户登录
     * @param user 用户
     * @return false 失败 true成功
     */
    public boolean login(User user) {

        List<User> list = userDao.readAll();
        boolean flag = false;

        for (User user1 : list) {
            //修改zy
            if (user.getUserName().equals(user1.getUserName())&&user.getPassWord().equals(user1.getPassWord()))
            {
                flag= true;
                break;
            }
        }
        return flag;
    }
}
